﻿using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Media;

using SBPweb.RIA.Client.Controls.Base;

namespace SBPweb.RIA.Client.Controls.Content
{

    public partial class HtmlFrame : HtmlDiv
    {

        #region Fields

        private WebBrowser browser;
        private HtmlElement iframe;

        #endregion

        #region Constructor logic

        public HtmlFrame()
            : base()
        {
            InitializeComponent();
        }

        #endregion

        #region Dependencies

        public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(Uri), typeof(HtmlFrame), null);

        #endregion

        #region Properties

        public Uri Url
        {
            get
            {
                return base.GetValue<Uri>(UrlProperty);
            }
            set
            {
                SetValue<Uri>(UrlProperty, value, "Uri");
                if (!ExtendableApplication.Current.IsRunningOutOfBrowser)
                {
                    iframe.SetProperty("src", value.OriginalString);
                }
                else
                {
                    browser.Source = value;
                }
            }
        }

        #endregion

        #region Methods

        protected override HtmlElement GetInnerElement()
        {
            iframe = HtmlPage.Document.CreateElement("iframe");
            iframe.SetStyleAttribute("width", "100%");
            iframe.SetStyleAttribute("height", "100%");
            iframe.SetStyleAttribute("border-left-width", "0px");
            iframe.SetStyleAttribute("border-top-width", "0px");
            iframe.SetStyleAttribute("border-right-width", "0px");
            iframe.SetStyleAttribute("border-bottom-width", "0px");
            iframe.SetProperty("frameborder", "no");
            return iframe;
        }

        protected override void InitializeControls()
        {
            if (!ExtendableApplication.Current.IsRunningOutOfBrowser)
            {
                base.InitializeControls();
            }
            else
            {
                browser = new WebBrowser();
                this.Content = browser;
            }
        }

        #endregion

        #region Event handlers

        #endregion

    }

}